/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Cali.java
 *
 * Created on 10-dic-2009, 18:52:06
 */
package GUI;

import Controlador.Controlador;
import Modelo.Alertas;
import Modelo.Idiomas.LocaleManager;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author Administrador
 */
public class GUI_Calibracion extends javax.swing.JDialog {

    private static Controlador controlador;
    private static JPanel mapa;
    private int modoCalibracion; // 1 = manual   |  2 = automatico
    private int puntoACalibrar; // 1 = Punto1   |  2 = Punto2
    private double lat1, lat2, long1, long2;
    private int pixelX1, pixelX2, pixelY1, pixelY2;
    private Cursor defaultCursor;
    private boolean punto1 = false;
    private boolean punto2 = false;

    /** Creates new form Cali */
    public GUI_Calibracion(JFrame parent) {
        super(parent, true);
        controlador = InicioAplicacion.getControlador();
        initComponents();
        mapa = PMapa;
        controlador.setDisplayMapa(mapa);
        cambioIdioma();
        activarLatLong(true);
        puntoACalibrar = 0;
        modoCalibracion = 1;
        parent.setVisible(false);
        centrarAplicacion();
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setVisible(true);
        defaultCursor = PMapa.getCursor();
    }

    public static JPanel getDisplayMapa() {
        return mapa;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        BGModoCalibracion = new javax.swing.ButtonGroup();
        JPCoordenadas = new javax.swing.JPanel();
        TXTLong1 = new javax.swing.JTextField();
        TXTLat1 = new javax.swing.JTextField();
        LLong = new javax.swing.JLabel();
        LLat = new javax.swing.JLabel();
        BPunto1 = new javax.swing.JButton();
        BPunto2 = new javax.swing.JButton();
        TXTLat2 = new javax.swing.JTextField();
        TXTLong2 = new javax.swing.JTextField();
        BDatosGps1 = new javax.swing.JButton();
        BDatosGps2 = new javax.swing.JButton();
        DPMapa = new javax.swing.JDesktopPane();
        PMapa = new javax.swing.JPanel();
        JPPixel = new javax.swing.JPanel();
        LabelX = new javax.swing.JLabel();
        LabelY = new javax.swing.JLabel();
        TXTPixelX = new javax.swing.JTextField();
        TXTPixelY = new javax.swing.JTextField();
        JDPCargarMapa = new javax.swing.JDesktopPane();
        BCargarMapa = new javax.swing.JButton();
        JDPCalibrar = new javax.swing.JDesktopPane();
        BCalibrar = new javax.swing.JButton();
        JDPSalir = new javax.swing.JDesktopPane();
        BSalir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("");
        setResizable(false);

        JPCoordenadas.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(102, 153, 255), 3, true), "Coordenadas", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Arial", 1, 14))); // NOI18N

        TXTLong1.setForeground(java.awt.Color.blue);
        TXTLong1.setVisible(false);

        TXTLat1.setForeground(java.awt.Color.blue);
        TXTLat1.setVisible(false);

        LLong.setText("Longitud:");
        LLong.setVisible(false);

        LLat.setText("Latitud:");
        LLat.setVisible(false);

        BPunto1.setText("Punto 1");
        BPunto1.setEnabled(false);
        BPunto1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BPunto1ActionPerformed(evt);
            }
        });

        BPunto2.setText("Punto 2");
        BPunto2.setEnabled(false);
        BPunto2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BPunto2ActionPerformed(evt);
            }
        });

        TXTLat2.setForeground(java.awt.Color.blue);
        TXTLat1.setVisible(false);

        TXTLong2.setForeground(java.awt.Color.blue);
        TXTLong1.setVisible(false);

        BDatosGps1.setText("Datos GPS");
        BDatosGps1.setEnabled(false);
        BDatosGps1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BDatosGps1ActionPerformed(evt);
            }
        });

        BDatosGps2.setText("Datos GPS");
        BDatosGps2.setEnabled(false);
        BDatosGps2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BDatosGps2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout JPCoordenadasLayout = new javax.swing.GroupLayout(JPCoordenadas);
        JPCoordenadas.setLayout(JPCoordenadasLayout);
        JPCoordenadasLayout.setHorizontalGroup(
            JPCoordenadasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JPCoordenadasLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(JPCoordenadasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(JPCoordenadasLayout.createSequentialGroup()
                        .addGroup(JPCoordenadasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(LLong)
                            .addComponent(LLat, javax.swing.GroupLayout.DEFAULT_SIZE, 63, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(JPCoordenadasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(BPunto1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(TXTLong1, javax.swing.GroupLayout.DEFAULT_SIZE, 110, Short.MAX_VALUE)
                            .addComponent(TXTLat1)))
                    .addComponent(BDatosGps1))
                .addGroup(JPCoordenadasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(JPCoordenadasLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(JPCoordenadasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(BPunto2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                            .addComponent(TXTLong2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                            .addComponent(TXTLat2, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)))
                    .addGroup(JPCoordenadasLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(BDatosGps2, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        JPCoordenadasLayout.setVerticalGroup(
            JPCoordenadasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JPCoordenadasLayout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(JPCoordenadasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(JPCoordenadasLayout.createSequentialGroup()
                        .addComponent(BPunto1)
                        .addGap(18, 18, 18)
                        .addComponent(TXTLat1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(TXTLong1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(JPCoordenadasLayout.createSequentialGroup()
                        .addComponent(BPunto2)
                        .addGap(18, 18, 18)
                        .addGroup(JPCoordenadasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(TXTLat2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(LLat))
                        .addGap(18, 18, 18)
                        .addGroup(JPCoordenadasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(TXTLong2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(LLong))
                        .addGap(15, 15, 15)
                        .addGroup(JPCoordenadasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(BDatosGps2)
                            .addComponent(BDatosGps1))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        DPMapa.setBackground(new java.awt.Color(235, 233, 237));
        DPMapa.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(102, 153, 255), 3, true), "Mapa", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Arial", 1, 14))); // NOI18N
        DPMapa.setForeground(new java.awt.Color(235, 233, 237));
        DPMapa.setPreferredSize(new java.awt.Dimension(650, 560));

        PMapa.setToolTipText("");
        PMapa.setName("Mapa"); // NOI18N
        PMapa.setPreferredSize(new java.awt.Dimension(633, 548));
        PMapa.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                PMapaMouseClicked(evt);
            }
        });
        PMapa.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                PMapaMouseMoved(evt);
            }
        });

        javax.swing.GroupLayout PMapaLayout = new javax.swing.GroupLayout(PMapa);
        PMapa.setLayout(PMapaLayout);
        PMapaLayout.setHorizontalGroup(
            PMapaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 633, Short.MAX_VALUE)
        );
        PMapaLayout.setVerticalGroup(
            PMapaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 548, Short.MAX_VALUE)
        );

        PMapa.setBounds(10, 20, 633, 548);
        DPMapa.add(PMapa, javax.swing.JLayeredPane.DEFAULT_LAYER);

        JPPixel.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(102, 153, 255), 3, true), "Pixel X,Y", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Arial", 1, 14))); // NOI18N

        LabelX.setText("X:");

        LabelY.setText("Y:");

        TXTPixelX.setEditable(false);
        TXTPixelX.setVisible(false);

        TXTPixelY.setEditable(false);
        TXTPixelY.setVisible(false);

        javax.swing.GroupLayout JPPixelLayout = new javax.swing.GroupLayout(JPPixel);
        JPPixel.setLayout(JPPixelLayout);
        JPPixelLayout.setHorizontalGroup(
            JPPixelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JPPixelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(LabelX, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(TXTPixelX, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(118, 118, 118)
                .addComponent(LabelY, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addComponent(TXTPixelY, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(35, 35, 35))
        );
        JPPixelLayout.setVerticalGroup(
            JPPixelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(JPPixelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(JPPixelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LabelX)
                    .addComponent(TXTPixelX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(TXTPixelY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(LabelY))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        JDPCargarMapa.setBackground(new java.awt.Color(235, 233, 237));
        JDPCargarMapa.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(102, 153, 255), 3, true), "", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Arial", 1, 14))); // NOI18N
        JDPCargarMapa.setForeground(new java.awt.Color(235, 233, 237));

        BCargarMapa.setText("Cargar Imagen");
        BCargarMapa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCargarMapaActionPerformed(evt);
            }
        });
        BCargarMapa.setBounds(87, 10, 170, 33);
        JDPCargarMapa.add(BCargarMapa, javax.swing.JLayeredPane.DEFAULT_LAYER);

        JDPCalibrar.setBackground(new java.awt.Color(235, 233, 237));
        JDPCalibrar.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(102, 153, 255), 3, true), "", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Arial", 1, 14))); // NOI18N
        JDPCalibrar.setForeground(new java.awt.Color(235, 233, 237));

        BCalibrar.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        BCalibrar.setForeground(java.awt.Color.red);
        BCalibrar.setText("CALIBRAR");
        BCalibrar.setEnabled(false);
        BCalibrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BCalibrarActionPerformed(evt);
            }
        });
        BCalibrar.setBounds(10, 10, 141, 35);
        JDPCalibrar.add(BCalibrar, javax.swing.JLayeredPane.DEFAULT_LAYER);

        JDPSalir.setBackground(new java.awt.Color(235, 233, 237));
        JDPSalir.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(102, 153, 255), 3, true), "", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.TOP, new java.awt.Font("Arial", 1, 14))); // NOI18N
        JDPSalir.setForeground(new java.awt.Color(235, 233, 237));

        BSalir.setFont(new java.awt.Font("Tahoma", 0, 18));
        BSalir.setText("Salir");
        BSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BSalirActionPerformed(evt);
            }
        });
        BSalir.setBounds(10, 10, 141, 35);
        JDPSalir.add(BSalir, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(DPMapa, javax.swing.GroupLayout.PREFERRED_SIZE, 653, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(JPCoordenadas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(JPPixel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(JDPCalibrar, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(JDPSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(JDPCargarMapa, javax.swing.GroupLayout.DEFAULT_SIZE, 351, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(DPMapa, javax.swing.GroupLayout.PREFERRED_SIZE, 578, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(JDPCargarMapa, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(JPPixel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(JPCoordenadas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(149, 149, 149)
                                .addComponent(JDPCalibrar, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(453, 453, 453)
                                .addComponent(JDPSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(40, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void PMapaMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_PMapaMouseMoved
        TXTPixelX.setText(String.valueOf(evt.getX()));
        TXTPixelY.setText(String.valueOf(evt.getY()));
    }//GEN-LAST:event_PMapaMouseMoved

    private void PMapaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_PMapaMouseClicked
        switch (puntoACalibrar) {
            case 1: {
                pixelX1 = evt.getX();
                pixelY1 = evt.getY();
                controlador.getCalibracion().setPixelX1(pixelX1);
                controlador.getCalibracion().setPixelY1(pixelY1);
                controlador.getCalibracion().paintBorder(null, PMapa.getGraphics(), 0, 0, 0, 0);
                punto1 = true;
                      if (!TXTLat1.getText().equals("") && !TXTLong1.getText().equals("") &&
                          !TXTLat2.getText().equals("") && !TXTLong2.getText().equals("") &&
                         punto1 && punto2 ){ activarCalibrar(true);}
                      else {activarCalibrar(false);}
                break;
            }
            case 2: {
                pixelX2 = evt.getX();
                pixelY2 = evt.getY();
                controlador.getCalibracion().setPixelX2(pixelX2);
                controlador.getCalibracion().setPixelY2(pixelY2);
                controlador.getCalibracion().paintBorder(null, PMapa.getGraphics(), 0, 0, 0, 0);
                punto2 = true;
                      if (!TXTLat1.getText().equals("") && !TXTLong1.getText().equals("") &&
                          !TXTLat2.getText().equals("") && !TXTLong2.getText().equals("") &&
                         punto1 && punto2 ){ activarCalibrar(true);}
                      else {activarCalibrar(false);}

                break;
            }
        }
    }//GEN-LAST:event_PMapaMouseClicked

    private void BCargarMapaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCargarMapaActionPerformed
        controlador.calibracion(this);
        activarPuntos(true);
    }//GEN-LAST:event_BCargarMapaActionPerformed

    private void BCalibrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BCalibrarActionPerformed
      if (!TXTLat1.getText().equals("") && !TXTLong1.getText().equals("") &&
          !TXTLat2.getText().equals("") && !TXTLong2.getText().equals("")) {
                    lat1 = Double.valueOf(TXTLat1.getText());
                    long1 = Double.valueOf(TXTLong1.getText());
                    controlador.getCalibracion().setPixelX1(pixelX1);
                    controlador.getCalibracion().setPixelY1(pixelY1);
                    controlador.getCalibracion().paintBorder(null, PMapa.getGraphics(), 0, 0, 0, 0);
                    lat2 = Double.valueOf(TXTLat2.getText());
                    long2 = Double.valueOf(TXTLong2.getText());
                    controlador.getCalibracion().setPixelX2(pixelX2);
                    controlador.getCalibracion().setPixelY2(pixelY2);
                    controlador.getCalibracion().paintBorder(null, PMapa.getGraphics(), 0, 0, 0, 0);
                  if ((Math.abs(lat1 - lat2) > 0.0001 ||
                       Math.abs(long1 - long2) > 0.0001)) {
                              controlador.getCalibracion().setGradosX1(long1);
                              controlador.getCalibracion().setGradosX2(long2);
                              controlador.getCalibracion().setGradosY1(lat1);
                              controlador.getCalibracion().setGradosY2(lat2);
                              controlador.getCalibracion().calibrar();
                              Alertas.exitoCalibracion(this);
                              //reiniciar();
                  } else {
                             Alertas.falloCalibracion(this);
                  }
        } else {
            Alertas.falloDatosCalibracion(this);
        }
        PMapa.setCursor(Cursor.getDefaultCursor());
    }//GEN-LAST:event_BCalibrarActionPerformed

    private void BPunto1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BPunto1ActionPerformed
        //Cambiamos el cursor
        Image imagenCursor = Toolkit.getDefaultToolkit().getImage(Toolkit.getDefaultToolkit().getClass().getResource("/Archivos/Imagenes/marcador.png"));
        Point hotSpot = new Point(0, 0);
        Cursor cursor = Toolkit.getDefaultToolkit().createCustomCursor(imagenCursor, hotSpot, "cursorMarcar");
        PMapa.setCursor(cursor);
        //---------------------------------------------------------------------
        activarLatLong(true);
        activarPixeles(true); 
        BPunto2.setForeground(Color.BLACK);
        BPunto2.setFont(new Font("Tahoma", Font.PLAIN, 11));
        BPunto1.setForeground(Color.red);
        BPunto1.setFont(new Font("Tahoma", Font.BOLD, 12));
        puntoACalibrar = 1;
        if (!TXTLat1.getText().equals("") && !TXTLong1.getText().equals("") &&
            !TXTLat2.getText().equals("") && !TXTLong2.getText().equals("") &&
             punto1 && punto2 ){ activarCalibrar(true);}
        else {activarCalibrar(false);}
        
    }//GEN-LAST:event_BPunto1ActionPerformed

    private void BPunto2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BPunto2ActionPerformed
        activarLatLong(true);
        activarPixeles(true);      
        BPunto1.setForeground(Color.BLACK);
        BPunto1.setFont(new Font("Tahoma", Font.PLAIN, 11));
        BPunto2.setForeground(Color.red);
        BPunto2.setFont(new Font("Tahoma", Font.BOLD, 12));
        puntoACalibrar = 2;
        if (!TXTLat1.getText().equals("") && !TXTLong1.getText().equals("") &&
        !TXTLat2.getText().equals("") && !TXTLong2.getText().equals("") &&
        punto1 && punto2 ){ activarCalibrar(true);}
        else {activarCalibrar(false);}
        
    }//GEN-LAST:event_BPunto2ActionPerformed

    private void BDatosGps2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BDatosGps2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_BDatosGps2ActionPerformed

    private void BSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BSalirActionPerformed
        dispose();
    }//GEN-LAST:event_BSalirActionPerformed

    private void BDatosGps1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BDatosGps1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_BDatosGps1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BCalibrar;
    private javax.swing.JButton BCargarMapa;
    private javax.swing.JButton BDatosGps1;
    private javax.swing.JButton BDatosGps2;
    private javax.swing.ButtonGroup BGModoCalibracion;
    private javax.swing.JButton BPunto1;
    private javax.swing.JButton BPunto2;
    private javax.swing.JButton BSalir;
    private javax.swing.JDesktopPane DPMapa;
    private javax.swing.JDesktopPane JDPCalibrar;
    private javax.swing.JDesktopPane JDPCargarMapa;
    private javax.swing.JDesktopPane JDPSalir;
    private javax.swing.JPanel JPCoordenadas;
    private javax.swing.JPanel JPPixel;
    private javax.swing.JLabel LLat;
    private javax.swing.JLabel LLong;
    private javax.swing.JLabel LabelX;
    private javax.swing.JLabel LabelY;
    private javax.swing.JPanel PMapa;
    private javax.swing.JTextField TXTLat1;
    private javax.swing.JTextField TXTLat2;
    private javax.swing.JTextField TXTLong1;
    private javax.swing.JTextField TXTLong2;
    private javax.swing.JTextField TXTPixelX;
    private javax.swing.JTextField TXTPixelY;
    // End of variables declaration//GEN-END:variables

    private void centrarAplicacion() {
        int width = this.getWidth();
        int height = this.getHeight();
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (screen.width - width) / 2;
        int y = (screen.height - height) / 2;
        setBounds(x, y, width, height);
    }

    private void activarLatLong(boolean t) {
        this.TXTLat1.setVisible(t);
        this.TXTLong1.setVisible(t);
        this.TXTLat2.setVisible(t);
        this.TXTLong2.setVisible(t);
        this.LLong.setVisible(t);
        this.LLat.setVisible(t);
    }

    private void activarPuntos(boolean t) {
        this.BPunto1.setEnabled(t);
        this.BPunto2.setEnabled(t);
    }

    private void activarPixeles(boolean t) {
        this.TXTPixelX.setVisible(t);
        this.TXTPixelY.setVisible(t);
    }

    private void activarCalibrar(boolean t) {
        this.BCalibrar.setEnabled(t);
    }

    private void reiniciar() {
        activarPuntos(false);
        activarPixeles(false);
        activarCalibrar(false);
        activarLatLong(false);
        controlador.getCalibracion().setPixelX1(-15);
        controlador.getCalibracion().setPixelY1(-15);
        controlador.getCalibracion().setPixelX2(-15);
        controlador.getCalibracion().setPixelY2(-15);
        PMapa.setBorder(null);
        PMapa.setCursor(defaultCursor);
    }

    private void cambioIdioma() {
        this.setTitle(LocaleManager.getMessage("calibracionTitulo"));
        this.BCalibrar.setText(LocaleManager.getMessage("calibracionCalibrar"));
        this.LLat.setText(LocaleManager.getMessage("calibracionLatitud"));
        this.LLong.setText(LocaleManager.getMessage("calibracionLongitud"));
        this.BCargarMapa.setText(LocaleManager.getMessage("calibracionCargar"));
        this.DPMapa.setName(LocaleManager.getMessage("calibracionMapa"));
        this.BSalir.setText(LocaleManager.getMessage("calibracionSalir"));
        this.BPunto1.setText(LocaleManager.getMessage("calibracionPunto1"));
        this.BPunto2.setText(LocaleManager.getMessage("calibracionPunto2"));
        this.BDatosGps1.setText(LocaleManager.getMessage("calibracionDatosGps"));
        this.BDatosGps2.setText(LocaleManager.getMessage("calibracionDatosGps"));
        this.JPCoordenadas.setBorder(
                javax.swing.BorderFactory.createTitledBorder(
                new javax.swing.border.LineBorder(
                new java.awt.Color(102, 153, 255), 3, true),
                LocaleManager.getMessage("calibracionCoordenadas"),
                javax.swing.border.TitledBorder.CENTER,
                javax.swing.border.TitledBorder.TOP,
                new java.awt.Font("Arial", 1, 14)));
        this.DPMapa.setBorder(
                javax.swing.BorderFactory.createTitledBorder(
                new javax.swing.border.LineBorder(
                new java.awt.Color(102, 153, 255), 3, true),
                LocaleManager.getMessage("calibracionMapa"),
                javax.swing.border.TitledBorder.CENTER,
                javax.swing.border.TitledBorder.TOP,
                new java.awt.Font("Arial", 1, 14)));
        this.JPPixel.setName(LocaleManager.getMessage("calibracionXY"));
    }
}
